﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using COFE.Core.Actions;

namespace COFE.Core.Plugin
{
    [Export(typeof(IPluginUser))]
    public class COFEActionPluginUser : PluginUserBase
    {
        public COFEActionPluginUser()
            : base(PluginBase.Priority_COFEACT)
        {

        }        

        protected T GetCOFEActionProperty<T>(IPluginManager manager, Func<ICOFEActionPlugin, T> propertyGetFunc)
        {
            return base.GetProperty<ICOFEActionPlugin, T>(manager, propertyGetFunc);
        }

        protected IEnumerable<T> GetAllCOFEActionProperty<T>(IPluginManager manager,
            Func<ICOFEActionPlugin, IEnumerable<T>> propertyGetFunc)
        {
            return base.GetAllProperty<ICOFEActionPlugin, T>(manager, propertyGetFunc);
        }

        public override void Apply(IPluginManager manager)
        {
            if (COFEActionShared.WorkScheduler != null)            
                COFEActionShared.WorkScheduler.Abort();            

            COFEActionShared.WorkScheduler =
                GetCOFEActionProperty(manager, p => p.WorkScheduler);
            var workEventLogger = GetCOFEActionProperty(manager, p => p.WorkEventLogger);
            if (workEventLogger != null)                            
                COFEShared.MasterEventLogger.RegisterEventLoggers(workEventLogger);                                        
        }
    }
}
